package com.pyue.eam.admin.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.pyue.eam.mp.config.WxMpInitConfigRunner;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;

import java.util.List;
import java.util.Map;

/**
 * 微信模板消息工具类
 *
 * @author Administrator
 * @date 2020/6/1 10:12
 */
public class WxPushMessageUtil {
    private static final String GZH_APP_ID = "wx143828a84a5dc0ba";
    private static final String MINI_APP_ID = "wxa9b73dc0c1c5c496";

    /**
     * 发送单人消息
     */
    public static void sendMessage(String toUser, String content) {
        sendContent(CollUtil.newArrayList(toUser), content);
    }

    public static void sendMessage(List<String> toUserList, String content) {
        sendContent(toUserList, content);
    }

    /**
     * 发送多人消息
     */
    private static void sendContent(List<String> toUserList, String content) {
        WxMpService wxMpService = WxMpInitConfigRunner.getMpServices().get(GZH_APP_ID);

        WxMpKefuMessage message = WxMpKefuMessage.TEXT().content(content).build();

        // 设置消息的内容等信息
        AsyncUtil.execute(() -> {

            for (String userOpenId : toUserList) {
                try {
                    message.setToUser(userOpenId);
                    wxMpService.getKefuService().sendKefuMessage(message);
                } catch (WxErrorException e) {
                    throw new RuntimeException(e);
                }
            }
        });


    }

    // public static void sendWxTempMes(String toUser, WXTaskTempEnum wxTaskTempEnum, Map<String, String> dataMap) {
    //     templateMessage(toUser, wxTaskTempEnum.getTempId(), dataMap);
    // }

    /**
     * 发送单人微信模板消息
     */
    public static void templateMessage(String toUser, String tempId, Map<String, String> dataMap) {
        templateMessage(CollUtil.newArrayList(toUser), tempId, dataMap, null);
    }

    /**
     * 发送多人微信模板消息
     */
    public static void templateMessage(List<String> toUserList, String tempId, Map<String, String> dataMap) {
        templateMessage(toUserList, tempId, dataMap, null);
    }

    private static void templateMessage(List<String> toUserList, String tempId, Map<String, String> dataMap, String miniPagePath) {
        miniPagePath = StrUtil.isBlank(miniPagePath) ? "pages/index/index" : miniPagePath;
        WxMpService wxMpService = WxMpInitConfigRunner.getMpServices().get(GZH_APP_ID);
        // 组装微信模板消息
        WxMpTemplateMessage templateMessage = getTemplateMsg(tempId, dataMap, miniPagePath);

        AsyncUtil.execute(() -> {
            // 遍历用户发送消息
            for (String toUser : toUserList) {
                try {
                    templateMessage.setToUser(toUser);
                    wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
                } catch (WxErrorException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    /**
     * 组装微信模板消息
     */
    private static WxMpTemplateMessage getTemplateMsg(String tempId, Map<String, String> dataMap, String miniPagePath) {
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                // 模板消息
                .templateId(tempId)
                // 优先跳转小程序
                .miniProgram(new WxMpTemplateMessage.MiniProgram(MINI_APP_ID, miniPagePath, false))
                // .url(url)
                .build();

        // 填充模板数据
        dataMap.forEach((key, value) -> templateMessage.addData(new WxMpTemplateData(key, value, "")));
        return templateMessage;
    }


    // 模板消息
    // .toUser("o4SHh6hBFp6dtFsZwGl4WR8pJUSw")
    // .templateId("xVgI6qrddeinqam0rkpFWhsThhaCAEbqYSLVE6ge380")
    // templateMessage.addData(new WxMpTemplateData("character_string4", "2023-12-12-002", ""));
    // templateMessage.addData(new WxMpTemplateData("thing17", "上海纳恩", ""));
    // templateMessage.addData(new WxMpTemplateData("phone_number26", "17752579193", ""));
    // templateMessage.addData(new WxMpTemplateData("thing9", "当前任务需要您尽快提供报价方案", ""));
    // templateMessage.addData(new WxMpTemplateData("time36", "2023-12-21", ""));

    // 订阅模板
    //.templateId("Us_tdkg17ETHrIlsXS6TT8S-Jd-I226XvjpDhWEpKOs")
    // templateMessage.addData(new WxMpTemplateData("thing4", "2023年度", ""));
    // templateMessage.addData(new WxMpTemplateData("thing2", "举办公司大会", ""));
    // templateMessage.addData(new WxMpTemplateData("time11", "2323.12.11 - 2323.12.14", ""));
    // templateMessage.addData(new WxMpTemplateData("thing7", "李磊", ""));
    // templateMessage.addData(new WxMpTemplateData("thing6", "测试消息", ""));
}
